package hoyo.zqy.product.service.impl;

import hoyo.zqy.basic.service.IBaiduAiAuditService;
import hoyo.zqy.basic.util.AjaxResult;
import hoyo.zqy.basic.util.LoginContext;
import hoyo.zqy.org.domain.Employee;
import hoyo.zqy.org.mapper.EmployeeMapper;
import hoyo.zqy.pet.mapper.PetOnlineAuditLogMapper;
import hoyo.zqy.product.domain.Product;
import hoyo.zqy.product.domain.ProductDetail;
import hoyo.zqy.product.domain.ProductOnlineAuditLog;
import hoyo.zqy.product.mapper.ProductDetailMapper;
import hoyo.zqy.product.mapper.ProductMapper;
import hoyo.zqy.product.mapper.ProductOnlineAuditLogMapper;
import hoyo.zqy.product.service.IProductService;
import hoyo.zqy.basic.service.impl.BaseServiceImpl;
import hoyo.zqy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    /**
     * 新增
     * @param product
     */
    @Override
    public void save(Product product) {
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        //进行明细保存
        if (productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    /**
     * 修改
      * @param product
     */
    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        //进行修改
        if (productDetail!=null){
            //修改的时候不修改id
            productDetailMapper.updateDetail(productDetail);
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        super.delete(id);
        productDetailMapper.delByProductId(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleBatch(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 上架
     * @param ids
     * @param request
     * @return
     */
    @Override
    public AjaxResult onSale(Long[] ids, HttpServletRequest request) {
        //获取当前登录信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //获取当前管理员id
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //获取所有上架id
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            //审核内容和图像
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            //如果都通过了
            if (boo1 && boo2){
                //更新数据
                product.setOnsaletime(new Date());
                product.setState(1L);
                productMapper.update(product);
                //添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setState(1);
                log.setNote("审核成功");
                log.setProductId(product.getId());
                log.setAuditId(employee.getId());
                productOnlineAuditLogMapper.save(log);
            }else {
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setState(0);
                log.setNote("审核失败");
                log.setProductId(product.getId());
                log.setAuditId(employee.getId());
                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("完啦，有好康的");
            }
        }
        return new AjaxResult();
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offSale(Long[] ids) {
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            //修改时间
            product.setOffsaletime(new Date());
            //修改状态
            product.setState((long) 0);
            //保存
            productMapper.update(product);
        }
    }
}
